<template>
  <FModal :loading="loading" header="配置项" ref="refFModal" @ok="ok" width="600">
    <el-form style="padding-right: 50px" ref="refForm" label-width="120px" :model="dictItem">
      <el-form-item prop="dictTitle" label="标题" :rules="required">
        <el-input v-focus placeholder="请输入标题" v-model="dictItem.dictTitle"/>
      </el-form-item>

      <el-form-item prop="dictValue" label="值" :rules="required">
        <el-input-number style="width: 100%"
                         placeholder="请输入值" v-model="dictItem.dictValue"/>
      </el-form-item>
    </el-form>
  </FModal>
</template>
<script setup>

import FModal from '@/components/FModal/index.vue';
import {ref, shallowRef} from 'vue';
import {isEmpty, real, required} from '@/utils/CommonUtils';
import {putAction} from '@/request/BaseRequest';

const refForm = shallowRef();
const refFModal = shallowRef();
const loading = shallowRef(false);
const emits = defineEmits(['ok']);
const dictItem = ref({});
let dictId = undefined;

function setData(data) {
  dictItem.value = JSON.parse(JSON.stringify(real(data)));
  open();
}

function addNew(_dictId) {
  dictId = _dictId;
  open();
  dictItem.value = {};
}

function open() {
  refFModal.value.open();
}

function ok() {
  refForm.value.validate((valid, errors) => {
    if (valid) {
      save();
    }
  });
}

function save() {
  loading.value = true;
  if (isEmpty(real(dictItem).dictId)) {
    real(dictItem).dictId = dictId;
  }
  putAction('/plat/platDictItem/save',
    real(dictItem)).then(data => {
    loading.value = false;
    if (data.success) {
      emits('ok');
      close();
    }
  });
}

function close() {
  refFModal.value.close();
}

defineExpose({
  addNew,
  setData,
});
</script>
<script>
export default {
  name: 'SysDictItemEditor'
};
</script>
<style>

</style>
